package P6;

import java.util.ArrayList;
import java.util.List;

public class P6_convert {

    public static void main(String[] args) throws Exception {

        System.out.println(new P6_convert().convert("aacabdkacaa", 2));
    }

    public String convert(String s, int numRows) {

        if (numRows == 1) {
            return s;
        }

        if (numRows > s.length()) {
            return s;
        }

        List<StringBuilder> result = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            result.add(new StringBuilder());
        }

        //Z的行数
        int zRowNum = 0;
        int flag = 1;
        for (int i = 0; i < s.length(); i++) {
            StringBuilder sb = result.get(zRowNum);
            sb.append(s.charAt(i));
            zRowNum = zRowNum + flag;
            if (zRowNum == 0 || zRowNum == numRows - 1) {
                flag = -flag;
            }
        }

        StringBuilder resultSb = new StringBuilder();
        for (StringBuilder sb : result) {
            resultSb.append(sb);
        }
        return resultSb.toString();
    }
}
